/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.coinche.client.business.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jb.statemachine.client.AbstractState;

public class Game extends AbstractState implements GameState {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6271013119493507500L;
	
	public Game() {
		players.add(null);
		players.add(null);
		players.add(null);
		players.add(null);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.business.model.GameState#getGame()
	 */
	public Game getGame() {
		return this;
	}
	
	/**
	 * 
	 * @return
	 */
	public List<Player> getPlayers() {
		return Collections.unmodifiableList( players );
	}
	
	/**
	 * 
	 * @param name
	 * @return
	 */
	public Player getPlayer( String name ) {
		for( Player p : players ) {
			if (p != null && p.getName().equals( name ))
				return p;
		}
		
		return null;
	}
	
	/*
	 * 
	 */
	public Player getNextPlayer( Player player ) {
		
		if (players.isEmpty())
			return null;
		
		if (player == null)
			return players.get(0);
		
		int i = players.indexOf( player );
		if (i == -1)
			return null;
		
		++i;
		
		if (i == players.size() )
			i = 0;
		
		return players.get( i );
	}
	
	/**
	 * 
	 * @param u
	 */
	public Player addUser( String name, int index ) {
		
		/*
		 * On recherche si un joueur existe deja
		 */
		
		for( Player user : players ) {
			if (user != null && user.getName().equals( name ) )
				throw new IllegalArgumentException("Joueur existant");
		}
		
		if (!isWaiting())
			throw new IllegalArgumentException( "Jeu complet" );
		
		if (players.get(index) != null)
			throw new IllegalArgumentException( "Place prise" );
		
		Player p = new Player();
		p.setName( name );
		p.setTeam( index % 2 == 0 ? Player.Team.T1 : Player.Team.T2 );
		players.set( index, p );
		
		return p;
	}
	
	/*
	 * 
	 */
	public boolean removeUser(String name) {
		for( int i = 0; i < players.size(); ++ i ) {
			Player p = players.get(i);
			
			if (p != null && name.equals( p.getName() )) {
				players.set(i, null);
				return true;
			}
		}
		
		return false;
		
	}
	
	/**
	 * 
	 * @return
	 */
	public int getScore1() {
		return score_team1;
	}
	
	/**
	 * 
	 * @param score
	 */
	public void addScore1( int score ) {
		score_team1 += score;
	}
	
	/**
	 * 
	 * @return
	 */
	public int getScore2() {
		return score_team2;
	}
	
	/**
	 * 
	 * @param score
	 */
	public void addScore2( int score ) {
		score_team2 += score;
	}

	/**
	 * @return the waiting
	 */
	public boolean isWaiting() {
		for( Player p : players ) {
			if (p == null)
				return true;
		}
		
		return false;
	}
	
	/**
	 * @param dealer the dealer to set
	 */
	public void setDealer(Player dealer) {
		this.dealer = dealer;
	}

	/**
	 * @return the dealer
	 */
	public Player getDealer() {
		return dealer;
	}

	private ArrayList<Player> players = new ArrayList<Player>(4);
	private Player dealer;
	private int score_team1 = 0;
	private int score_team2 = 0;


}
